package com.shudes.util;

import java.text.*;
import java.util.*;

public class Dates {

	public static final SimpleDateFormat MMDDYYYY_SLASH = new SimpleDateFormat("MM/dd/yyyy");
	public static final SimpleDateFormat MMDDYYYY_DASH = new SimpleDateFormat("MM-dd-yyyy"); 
	public static final SimpleDateFormat TIMESTAMP = new SimpleDateFormat("MM-dd-yyyy HH:mm");

	private Dates() {
	}
	
	public static Date create(String mmddyyyy) {
		Date result;
		Set<SimpleDateFormat> s; 
		
		s = new HashSet<SimpleDateFormat>();
		s.add(MMDDYYYY_SLASH); 
		s.add(MMDDYYYY_DASH); 
		result=null;
		
		for(SimpleDateFormat formatter : s) {
			try {
				result = formatter.parse(mmddyyyy);
				return result; 
			} catch (ParseException e) {};
		}		
		return null;
	}
	
	public static Date truncateDate(Date d) { 
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		c.set(Calendar.HOUR,0);
		c.set(Calendar.MINUTE, 0); 
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		return c.getTime();
	}
	
	public static Date endOfDay(Date d, int daysOffset) {
		Calendar c = Calendar.getInstance();
		c.setTime(d); 
		c.add(Calendar.DATE, daysOffset);
		return endOfDay(c.getTime());
	}
	
	public static Date endOfDay(Date d) {
		Calendar c=Calendar.getInstance();
		c.setTime(d); 
		c.set(Calendar.HOUR, 23); 
		c.set(Calendar.MINUTE, 59); 
		c.set(Calendar.SECOND, 59); 
		c.set(Calendar.MILLISECOND, 0); 
		return c.getTime();
	}

}
